import 'package:chop/Model/order_entity.dart';
import 'package:chop/common/DioUtils.dart';
import 'package:chop/common/SPUtils.dart';
import 'package:chop/view/order/order_item.dart';
import 'package:chop/widgets/TextSizeWidget.dart';
import 'package:chop/widgets/appBar.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/ball_pulse_footer.dart';
import 'package:flutter_easyrefresh/ball_pulse_header.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:get/get.dart';
import 'package:loading_animations/loading_animations.dart';

// ignore: camel_case_types
class orderPage extends StatefulWidget {
  final int type;

  // ignore: sort_constructors_first
  const orderPage({Key key, this.type}) : super(key: key);

  @override
  _orderPageState createState() => _orderPageState();
}

// ignore: camel_case_types
class _orderPageState extends State<orderPage> {
  // false -> 未完成订单 true -> 已完成订单
  int type;

  // 是否在网络请求中
  bool isNetworkNotBad = false;

  int code = 0;
  //当前页码
  int pageNum = 0;

  //是否还有更多页
  bool isNoMore = true;

  List<orderData> data = [];

  // EasyRefresh控制器
  EasyRefreshController easyRefreshController = EasyRefreshController();

  Widget getBody() {
    if (isNetworkNotBad) {
      print("加载完成");
      if(data.length != 0){
        if(code == 0){
          return ListView.builder(
            itemBuilder: (context, index) {
              return getOrderItem(context,easyRefreshController: easyRefreshController,data: data[index], type: type);
            },
            itemCount: data.length,
          );
        }else{
          return Center(child: FixedSizeText("服务器连接失败"),);
        }

      }else{
        return Center(child: FixedSizeText("暂无订单"),);
      }

    } else {
      print("正在加载");
      return Center(child: LoadingJumpingLine.square());
    }
  }

  Future<void> getData(type) async {
    String UID = await SPUtils.get("UID");
    orderData a = orderData();
    a.fromId = int.parse(UID);
    a.status = "$type";
    await DioUtils.orderList(a).then((Map result) {
      if (result['code']!=500) {
        result['data'].forEach((v){
          data.add(orderData.fromJson(v));
        });
        setState(() {
          isNetworkNotBad = true;
        });
      }else{
        setState(() {
          isNetworkNotBad = true;
          code = 500;
        });
      }
    });
  }

  @override
  void initState() {
    this.type = widget.type;
    getData(type);
    // TODO: implement initState
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: BaseViewBar(
        childView: BaseTitleBar(
          getTitle(type),
          leftClick: () {
            Get.back();
          },
        ),
        preferredSize: Size.fromHeight(50.0),
      ),
      backgroundColor: Color.fromRGBO(212, 212, 212, 1),
      body: Container(
          child: Column(
        children: <Widget>[
          Expanded(
            child: EasyRefresh(
              controller: easyRefreshController,
              header: BallPulseHeader(),
              footer: BallPulseFooter(),
              child: getBody(),
              onRefresh: () async {
                setState(() {
                  pageNum = 1;
                });
                data = [];
                await getData(type);
              },
              onLoad: isNoMore
                  ? null
                  : () async {
                      pageNum += 1;
                      await getData(type);
                    },
            ),
          ),
        ],
      )),
    );
  }

  String getTitle(int type) {
    switch (type) {
      case 0:
        return "进行中订单";
      case 1:
        return "已完成订单";
      case 2:
        return "未支付订单";
      case 3:
        return "待接单";
    }
  }
}
